package com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.repository.HddcWyGeologicalsvylineNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.repository.HddcWyGeologicalsvylineRepository;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.repository.entity.HddcWyGeologicalsvylineEntity;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.service.HddcWyGeologicalsvylineService;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.viewobjects.HddcWyGeologicalsvylineQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyGeologicalSvyLine.viewobjects.HddcWyGeologicalsvylineVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zyb
 * @date 2020-12-01
 */
@Service
public class HddcWyGeologicalsvylineServiceImpl implements HddcWyGeologicalsvylineService {

	@Autowired
    private HddcWyGeologicalsvylineRepository hddcWyGeologicalsvylineRepository;
    @Autowired
    private HddcWyGeologicalsvylineNativeRepository hddcWyGeologicalsvylineNativeRepository;

    @Override
    public JSONObject queryHddcWyGeologicalsvylines(HddcWyGeologicalsvylineQueryParams queryParams , int curPage,
                                                    int pageSize ) {
        Page<HddcWyGeologicalsvylineEntity> hddcWyGeologicalsvylinePage =
                this.hddcWyGeologicalsvylineNativeRepository.queryHddcWyGeologicalsvylines(queryParams , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyGeologicalsvylinePage);
        return jsonObject;
    }


    @Override
    public HddcWyGeologicalsvylineEntity getHddcWyGeologicalsvyline(String id) {
        HddcWyGeologicalsvylineEntity hddcWyGeologicalsvyline = this.hddcWyGeologicalsvylineRepository.findById(id).orElse(null);
         return hddcWyGeologicalsvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvylineEntity saveHddcWyGeologicalsvyline(HddcWyGeologicalsvylineEntity hddcWyGeologicalsvyline) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyGeologicalsvyline.setId(uuid);
        hddcWyGeologicalsvyline.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvyline.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        hddcWyGeologicalsvyline.setIsValid("1");
        this.hddcWyGeologicalsvylineRepository.save(hddcWyGeologicalsvyline);
        return hddcWyGeologicalsvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyGeologicalsvylineEntity updateHddcWyGeologicalsvyline(HddcWyGeologicalsvylineEntity hddcWyGeologicalsvyline) {
        hddcWyGeologicalsvyline.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyGeologicalsvyline.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyGeologicalsvylineRepository.save(hddcWyGeologicalsvyline);
        return hddcWyGeologicalsvyline;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyGeologicalsvylines(List<String> ids) {
        List<HddcWyGeologicalsvylineEntity> hddcWyGeologicalsvylineList = this.hddcWyGeologicalsvylineRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyGeologicalsvylineList) && hddcWyGeologicalsvylineList.size() > 0) {
            for(HddcWyGeologicalsvylineEntity hddcWyGeologicalsvyline : hddcWyGeologicalsvylineList) {
//                this.hddcWyGeologicalsvylineRepository.delete(hddcWyGeologicalsvyline);
                hddcWyGeologicalsvyline.setIsValid("0");
                hddcWyGeologicalsvyline.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcWyGeologicalsvyline.setUpdateTime(new Date());
                this.hddcWyGeologicalsvylineRepository.save(hddcWyGeologicalsvyline);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

    @Override
    public BigInteger queryHddcWyGeologicalsvyline(HddcWyGeologicalsvylineQueryParams queryParams) {
        BigInteger bigInteger = hddcWyGeologicalsvylineNativeRepository.queryHddcWyGeologicalsvyline(queryParams);
        return bigInteger;
    }

}
